<?php
class Plan_Model_PlanRates
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('非正确的table类型！');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Plan_Model_DbTable_PlanRates');
        }
        return $this->_dbTable;
    }

    //获取费率数据
    public function getRateByTypeAndQty($type, $qty)
    {
        $select = $this->getDbTable()->select()->where("type = ?",$type)
                                               ->where("range_end <= ? or (range_start < ? and range_end >= ?)", $qty)
                                               ->order('order');
        return  $this->_dbTable->fetchAll($select);
    }
    
    
    //按类型获取标准价格
    public function getListPriceByType($type)
    {
    	$select = $this->getDbTable()->select()->from($this->_dbTable,array('price'))
    	                                       ->where("type = ?",$type);
    	return  $this->_dbTable->fetchRow($select);
    }

	public function add($row){
		return $this->getDbTable()->insert($row);
	}

	public function delete($id){
		$where = $this->getDbTable()->getAdapter()->quoteInto('pr_id = ?',$id); 
		return $this->_dbTable->delete($where);
	}	
	
    public function update($row,$id)
    {
    	$where = $this->getDbTable()->getAdapter()->quoteInto('pr_id = ?',$id);
 
		return $this->_dbTable->update($row, $where);
    }
	
	//按ID获取标准套餐详细
    public function getById($pr_id)
    {
        $select = $this->getDbTable()->select()->where("pr_id = ?",$pr_id);
        return  $this->_dbTable->getAdapter()->fetchRow($select);
    }
	//列表
    public function getByCondition($condition=array(),$type='*',$orderby="pr_id",$pageSize=0,$page=0){
		$select = $this->getDbTable()->select()->from($this->_dbTable,$type);
        $select->where("1 =?",1);
		if(isset($condition["type"]) && $condition["type"]!=''){
            $select->where("type =?",$condition["type"]);
        }
        if('count(*)'==$type){
            return $this->_dbTable->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_dbTable->getAdapter()->fetchAll($sql);
        }
	}

}